/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * InterfazCrearArea.java
 *
 * Created on 27/10/2011, 10:25:41 PM
 */
package presentacion;

import Paneles.PanelGeneral;
import java.awt.BorderLayout;
import business.AreaBO;
import dominio.Area;
import javax.swing.JOptionPane;
import javax.swing.ImageIcon;

/**
 *
 * @author SEBASTIAN
 */
public class InterfazCrearArea extends javax.swing.JFrame {

    private ImageIcon icono;

    /** Creates new form InterfazCrearArea */
    public InterfazCrearArea() {
        initComponents();
        PanelGeneral panel = new PanelGeneral();
        this.add(panel, BorderLayout.SOUTH);
        icono = new ImageIcon(getClass().getResource("/Imagenes/icon.gif"));
        this.setIconImage(icono.getImage());
        
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        entrada1 = new javax.swing.JTextField();
        crearArea = new javax.swing.JButton();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        entrada2 = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();

        setTitle("Creación de Areas");
        setResizable(false);

        jLabel1.setText("Nombre del área nuevo:");

        crearArea.setText("Crear Area");
        crearArea.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                crearAreaActionPerformed(evt);
            }
        });

        jLabel2.setText("Para asignar nuevas asignaturas al area debe crearlas y asignarlas alli");

        jLabel3.setText("Descripción:");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel3)
                            .addComponent(jLabel1))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 255, Short.MAX_VALUE)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(entrada1)
                            .addComponent(entrada2, javax.swing.GroupLayout.DEFAULT_SIZE, 308, Short.MAX_VALUE)))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel4)
                            .addComponent(jLabel5)
                            .addGroup(layout.createSequentialGroup()
                                .addGap(17, 17, 17)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addGroup(layout.createSequentialGroup()
                                        .addGap(107, 107, 107)
                                        .addComponent(crearArea, javax.swing.GroupLayout.PREFERRED_SIZE, 231, javax.swing.GroupLayout.PREFERRED_SIZE))
                                    .addComponent(jLabel2))))
                        .addGap(0, 0, Short.MAX_VALUE)))
                .addContainerGap())
        );

        layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {jLabel1, jLabel3});

        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel4)
                    .addComponent(jLabel5))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(entrada1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(entrada2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addComponent(crearArea)
                .addGap(18, 18, 18)
                .addComponent(jLabel2)
                .addGap(27, 27, 27))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void crearAreaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_crearAreaActionPerformed
        int ok=0;
        AreaBO bo = new AreaBO();
        Area tst;
        if (entrada1.getText().equals("") || entrada2.getText().equals("")) {
            JOptionPane.showMessageDialog(this, "Debe digitar los datos completos para crear un Area", null, JOptionPane.ERROR_MESSAGE);
        }
        else {
             tst = bo.consultarArea(entrada1.getText()+"");
               
            if (tst == null) {
                ok = bo.crearArea(entrada1.getText(), entrada2.getText());
                entrada1.setText("");
                entrada2.setText("");//mar
            }
            if (ok == 1) {
                JOptionPane.showMessageDialog(this, "La creacion del area fue exitosa", "Creacion exitosa !!!", JOptionPane.INFORMATION_MESSAGE);
            }
            else {
                if (tst !=null)
                    JOptionPane.showMessageDialog(this, "Ocurrio un error al crear el area (Nombres repetidos)", "Creacion fallida", JOptionPane.ERROR_MESSAGE);
                else{
                    JOptionPane.showMessageDialog(this, "Ocurrio un error al crear el area", "Creacion fallida", JOptionPane.ERROR_MESSAGE);
                }
            }
            // this.setVisible(false);t
        }
    }//GEN-LAST:event_crearAreaActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new InterfazCrearArea().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton crearArea;
    private javax.swing.JTextField entrada1;
    private javax.swing.JTextField entrada2;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    // End of variables declaration//GEN-END:variables
}
